<?
$first = FALSE;
?>
<html>
	<head>
	<style>
	* {
		font-family:Frutiger, Helvetica, Arial, sans-serif;
		padding: 0px;
		margin: 0px;
	}

	
	table
	{
		border: 2px solid black;
	}

	th
	{
		border-left: 1px solid black;
		padding: 2px;
		font-size: 70%;
		color: #900;
		
	}

	td
	{
		border-left: 1px solid black;
		border-top: 1px solid black;
		padding: 2px;
		font-size: 70%;
	}

	input, select {
		font-weight: bold;
		padding: 2px;
		font-size: 115%;
	}
	textarea
	{
		padding: 8px;
		font-family: monospace;
		font-size: 80%;
	}

	h1, h2, h3, h4
	{
		margin-top: 1em;
		margin-bottom: 1em;
	}
	/* Scaffold View */
dl {
	line-height: 2em;
	margin: 0em 0em;
}
dl.altrow {
	background: #f4f4f4;
}
dt {
	font-weight: bold;
	padding-left: 4px;
	vertical-align: top;
}
dd {
	margin-left: 10em;
	margin-top: -2em;
	margin-right: 1em;
	vertical-align: top;
	margin-bottom: 0.5em;
}

body { padding: 1em; }
	</style>
	</head>
	<body>


<table>
<? foreach($this->table_def_display as $column):
if (!$first): ?>
	<tr>
		<th><?= ucfirst(strtolower(str_replace('_', ' ',
					join('</th><th>', array_keys($column))))) ?></th>
	</tr>
<? $first = TRUE;
endif; ?>
	<tr>
		<td>
			<?= join('&nbsp;</td><td>', $column) ?>&nbsp;
		</td>
	</tr>
<? endforeach; ?>
</table>
<h2>Table Definition</h2>
<form method="post" action="<?= ZUPAL_BASEURL ?>/admin/modules/tablewrite/" >
<input type="hidden" name="table_class_name" value="<?= $this->table_class_name ?>" />
	<textarea rows="10" cols="60" name="class_file">
<? ob_start(); ?>
	 Zend_Db_Table_Abstract::getDefaultAdapter()->query("<?= $this->create_sql ?>");
<? $body = ob_get_clean();


	$create_method = new Zend_CodeGenerator_Php_Method(
		array(
			'name' => 'create_table',
			'visibility' => 'public',
			'static' => TRUE,
			'body' => $body
		)
	);
	
	$create_method->setStatic(TRUE);

	ob_start();
	?>
	if(!$this->table_exists()) $this->create_table();
	<?
	$body = ob_get_clean();

	$init_method = new Zend_CodeGenerator_Php_Method(
		array(
			'name' => 'init',
			'visibility' => 'protected',
			'body' => $body
		)
	);

//	$class->set
	/*
	 *
	protected $_id_field = 'mb_id';
	protected $_name = 'zupal_media_mb_artists';
	 */
	$id_prop = new Zend_CodeGenerator_Php_Property(
		array(
		'name' => '_id_field',
		'defaultValue' => $this->id_field,
		'visibility' => 'protected'
		)
	);

	$name_prop = new Zend_CodeGenerator_Php_Property(
		array(
		'name' => '_name',
		'defaultValue' => $this->table_name,
		'visibility' => 'protected'
		)
	);
	
	$class = new Zend_CodeGenerator_Php_Class(
		array(
			'name' => $this->table_class_name,
			'extendedClass' => 'Zupal_Table_Abstract',
			'methods' => array(
				$create_method// , $init_method
			),
			'properties' => array($id_prop, $name_prop)
		)
	);
	if ($this->db_name):

		$const = new Zend_CodeGenerator_Php_Method(
			array(
				'name' => '__construct',
				'body' => '        parent::__construct(array("db" => Zupal_Module_Manager::getInstance()->database("' .  $this->db_name . '")));'
			)
		);
		$class->setMethod($const);
	endif;

/*
	$class->setProperties(
		array(
			$id_prop,
			$name_prop
		)

	); */

	$file = new Zend_CodeGenerator_Php_File(
		array(
			'classes' => array(
				$class
			)
		)
	);

	echo $file;
?>
	</textarea>
	<input type="hidden" name="table" value="<?= $this->table_name ?>" />
<dl>
	<dt>
		Module
	</dt>
	<dd>
		<select name="table_module">
			<option value="" selected="selected">Choose a Module</option>
		<? foreach (Zupal_Module_Manager::getInstance()->getModuleNames() as $m): ?>
			<option value="<?= $m ?>"><?= ucfirst($m) ?></option>

		<? endforeach ?>

		</select>
	</dd>
	<dt>&nbsp;</dt>
	<dd>
		<input type="submit" value="Create Table Class" />
	</dd>
	<dt>Make Module File</dt>
	<dd>
		<input type="checkbox" name="make_domain" />
	</dd>
	
</dl>

<input type="hidden" name="module_class_name" value="<?= $this->module_class ?>" />
<textarea name="module_file" rows="10" cols="60" >
<?

	$module_get = new Zend_CodeGenerator_Php_Method(
		array(
			'name' => 'get',
			'body' => 'return new self($pID);',
			'parameters' => array(array('name' => 'pID'))
		)
	);

	$module_tableClass = new Zend_CodeGenerator_Php_Method(
		array(
			'name' => 'tableClass',
			'body' => "return '{$this->table_class_name}';"
		)
	);
/*
	private static $_Instance = NULL;

/**
 *
 * @return Zupal_People
 *
	public static function getInstance()
	{

	} */

	$module_instance_prop = new Zend_CodeGenerator_Php_Property(
		array(
			'static' => TRUE,
			'visibility' => 'protected static',
			'name' => '_Instance'
		)		
	);

	ob_start();
?>
		if (is_null(self::$_Instance)):
	self::$_Instance = new self();
endif;
return self::$_Instance;
<?
	$body = ob_get_clean();

	$module_instance_method = new Zend_CodeGenerator_Php_Method(
		array(
			'name' => 'getInstance',
			'body' => $body,
			'visibility' => 'public static'
		)
	);

	$module_class = new Zend_CodeGenerator_Php_Class(
		array(
			'name' => $this->module_class,
			'extendedClass' => 'Zupal_Domain_Abstract',
			'methods' => array(
				$module_get,
				$module_tableClass,
				$module_instance_method
			),
			'properties' => array($module_instance_prop)
		)
	);



	$module_file = new Zend_CodeGenerator_Php_File(
		array(
			'classes' => array(
				$module_class
			)
		)
	);

	echo $module_file;
?>
</textarea>

</form>
</body>
</html>